/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.meanzoft.minecraft_clone.effect;

import org.meanzoft.m3d.scene.SceneNode;
import org.lwjgl.util.vector.Vector3f;
import org.meanzoft.m3d.scene.Renderable;

import static org.lwjgl.opengl.GL11.*;

/**
 *
 * @author Meanz
 */
public class Fire extends SceneNode implements Renderable {

    private Particle[] particles;
    private long lastUpdate;
    private float maxSize = 2.5f;

    public Fire() {
        particles = new Particle[100];
        for (int i = 0; i < particles.length; i++) {
            particles[i] = new Particle();
            createParticle(i);
        }
        lastUpdate = System.currentTimeMillis();
    }

    public void createParticle(int particleId) {
        float ranX = random(maxSize);
        float ranZ = random(maxSize);
        particles[particleId].setPosition(ranX < maxSize / 2 ? -ranX : ranX, 0f, ranZ < maxSize / 2 ? -ranZ : ranZ);
        particles[particleId].setAngle(random(240), 0f, -random(180));
    }

    public void update() {
        if (System.currentTimeMillis() - lastUpdate > 100) {
            for (int i = 0; i < particles.length; i++) {
                particles[i].update(i);
            }
            lastUpdate = System.currentTimeMillis();
        }
    }

    public void render() {
        glTranslatef(-0.25f, 0f, -0.25f);
        glDisable(GL_CULL_FACE);
        glDisable(GL_TEXTURE_2D);
        glScalef(0.1f, 0.1f, 0.1f);
        glBegin(GL_QUADS);
        {
            for (int i = 0; i < particles.length; i++) {
                glColor4f(1f, particles[i].position.y * 0.3f, 0f, 1 - (particles[i].position.y * 0.1f));
                glVertex3f(particles[i].position.x, particles[i].position.y, particles[i].position.z);
                glVertex3f(particles[i].position.x + 1, particles[i].position.y, particles[i].position.z);
                glVertex3f(particles[i].position.x + 1, particles[i].position.y + 1, particles[i].position.z);
                glVertex3f(particles[i].position.x, particles[i].position.y + 1, particles[i].position.z);
            }
        }
        glEnd();
        glEnable(GL_CULL_FACE);
        glEnable(GL_TEXTURE_2D);
    }

    public float random(float maxValue) {
        return (float) Math.random() * maxValue;
    }

    class Particle {

        private Vector3f position = new Vector3f(0f, 0f, 0f);
        private Vector3f angle = new Vector3f(0f, 1f, 0f);
        private float stepDistance = 0.4f;
        

        public Particle() {
        }

        public void setAngle(float x, float y, float z) {
            angle.x = x;
            angle.y = y;
            angle.z = z;
        }

        public void setPosition(float x, float y, float z) {
            position.x = x;
            position.y = y;
            position.z = z;
        }

        public void update(int id) {
            if (position.x > maxSize || position.y > maxSize * 2 || position.z > maxSize
                    || position.x < -maxSize || position.y < -maxSize || position.z < -maxSize) {
                createParticle(id);
                return;
            }
            position.x -= stepDistance * (float) Math.sin(Math.toRadians(angle.x));
            position.z += stepDistance * (float) Math.cos(Math.toRadians(angle.x));
            position.y -= stepDistance * (float) Math.sin(Math.toRadians(angle.z));
        }
    }
}
